@{
    ViewData["Title"] = "生产报工";
    var isViewMode = ViewBag.IsViewMode ?? false;
    var pageTitle = isViewMode ? "查看生产报工" : (Model?.Id > 0 ? "编辑生产报工" : "新增生产报工");
}
@model ThridGroup.ERP.Application.Contracts.DTO.ProductionReportingDto

<!-- 直接引入所有必要的CSS和JS文件 -->
<link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet" />
<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>

<!-- 自定义样式 -->
<style>
    body {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        min-height: 100vh;
        font-family: 'Microsoft YaHei', sans-serif;
    }
    
    .production-reporting-container {
        padding: 20px;
        min-height: 100vh;
    }
    
    .page-header {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 15px;
        padding: 25px;
        margin-bottom: 20px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.2);
    }
    
    .page-title {
        font-size: 28px;
        font-weight: 600;
        color: #2c3e50;
        margin: 0;
        display: flex;
        align-items: center;
        gap: 15px;
    }
    
    .page-title i {
        color: #3498db;
        font-size: 32px;
    }
    
    .page-subtitle {
        color: #7f8c8d;
        font-size: 14px;
        margin-top: 8px;
        margin-left: 47px;
    }
    
    .form-card {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 15px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        margin-bottom: 20px;
        padding: 30px;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.2);
        transition: all 0.3s ease;
    }
    
    .form-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
    }
    
    .form-section-title {
        font-size: 18px;
        font-weight: 600;
        color: #2c3e50;
        margin-bottom: 25px;
        padding-bottom: 10px;
        border-bottom: 2px solid #3498db;
        display: flex;
        align-items: center;
        gap: 10px;
    }
    
    .form-section-title i {
        color: #3498db;
        font-size: 20px;
    }
    
    .form-row {
        display: flex;
        margin-bottom: 20px;
        align-items: flex-start;
        gap: 20px;
    }
    
    .form-group {
        flex: 1;
        display: flex;
        flex-direction: column;
    }
    
    .form-label {
        color: #34495e;
        font-weight: 600;
        margin-bottom: 8px;
        font-size: 14px;
        display: flex;
        align-items: center;
        gap: 5px;
    }
    
    .form-label.required::after {
        content: '*';
        color: #e74c3c;
        margin-left: 3px;
    }
    
    .form-input, .form-textarea {
        border: 2px solid #ecf0f1;
        border-radius: 8px;
        padding: 12px 15px;
        font-size: 14px;
        transition: all 0.3s ease;
        background: #fff;
        color: #2c3e50;
    }
    
    .form-input {
        height: 45px;
    }
    
    .form-textarea {
        min-height: 80px;
        resize: vertical;
        font-family: inherit;
    }
    
    .form-input:focus, .form-textarea:focus {
        border-color: #3498db;
        box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
        outline: none;
    }
    
    .form-input:disabled, .form-textarea:disabled {
        background: linear-gradient(135deg, #f8f9fa, #e9ecef);
        color: #6c757d;
        cursor: not-allowed;
        border-color: #dee2e6;
    }
    
    .btn-group {
        text-align: center;
        margin-top: 30px;
        padding-top: 20px;
        border-top: 2px solid #ecf0f1;
    }
    
    .btn {
        padding: 12px 30px;
        margin: 0 10px;
        border: none;
        border-radius: 8px;
        cursor: pointer;
        font-size: 14px;
        font-weight: 600;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 8px;
        text-decoration: none;
        min-width: 120px;
        justify-content: center;
    }
    
    .btn-primary {
        background: linear-gradient(135deg, #27ae60, #2ecc71);
        color: white;
        box-shadow: 0 4px 15px rgba(39, 174, 96, 0.3);
    }
    
    .btn-primary:hover {
        background: linear-gradient(135deg, #229954, #27ae60);
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(39, 174, 96, 0.4);
    }
    
    .btn-primary:disabled {
        background: #95a5a6;
        cursor: not-allowed;
        transform: none;
        box-shadow: none;
    }
    
    .btn-default {
        background: #ecf0f1;
        color: #34495e;
        border: 2px solid #bdc3c7;
    }
    
    .btn-default:hover {
        background: #d5dbdb;
        border-color: #95a5a6;
        transform: translateY(-2px);
    }
    
    .tabs-container {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 15px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        margin-top: 20px;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.2);
        overflow: hidden;
    }
    
    .tabs-header {
        display: flex;
        background: linear-gradient(135deg, #667eea, #764ba2);
        position: relative;
    }
    
    .tab-item {
        padding: 18px 30px;
        cursor: pointer;
        color: rgba(255, 255, 255, 0.8);
        font-weight: 500;
        transition: all 0.3s ease;
        position: relative;
        flex: 1;
        text-align: center;
    }
    
    .tab-item:hover {
        color: white;
        background: rgba(255, 255, 255, 0.1);
    }
    
    .tab-item.active {
        color: white;
        background: rgba(255, 255, 255, 0.2);
    }
    
    .tab-item.active::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        height: 3px;
        background: #fff;
    }
    
    .tab-content {
        padding: 30px;
        min-height: 300px;
    }
    
    .tab-pane {
        display: none;
        animation: fadeIn 0.3s ease;
    }
    
    .tab-pane.active {
        display: block;
    }
    
    @@keyframes fadeIn {
        from { opacity: 0; transform: translateY(10px); }
        to { opacity: 1; transform: translateY(0); }
    }
    
    .empty-data {
        text-align: center;
        color: #7f8c8d;
        padding: 60px 0;
        font-size: 16px;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 15px;
    }
    
    .empty-data i {
        font-size: 48px;
        color: #bdc3c7;
    }
    
    .table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 10px;
        background: #fff;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    }
    
    .table th,
    .table td {
        padding: 15px 12px;
        text-align: center;
        border-bottom: 1px solid #ecf0f1;
    }
    
    .table th {
        background: linear-gradient(135deg, #f8f9fa, #e9ecef);
        font-weight: 600;
        color: #2c3e50;
        font-size: 14px;
    }
    
    .table tbody tr {
        transition: all 0.3s ease;
    }
    
    .table tbody tr:hover {
        background: linear-gradient(135deg, #ebf3fd, #d6eaff);
        transform: scale(1.01);
    }
    
    .table tbody tr:last-child td {
        border-bottom: none;
    }
    
    /* 输入框图标 */
    .input-with-icon {
        position: relative;
    }
    
    .input-with-icon i {
        position: absolute;
        left: 12px;
        top: 50%;
        transform: translateY(-50%);
        color: #7f8c8d;
        z-index: 1;
    }
    
    .input-with-icon .form-input {
        padding-left: 40px;
    }
    
    /* 数量输入框特殊样式 */
    .quantity-input-group {
        display: flex;
        align-items: center;
        gap: 10px;
    }
    
    .quantity-input-group .form-input {
        flex: 1;
    }
    
    .quantity-btn {
        width: 40px;
        height: 40px;
        border: none;
        border-radius: 6px;
        background: linear-gradient(135deg, #3498db, #2980b9);
        color: white;
        font-size: 16px;
        font-weight: bold;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .quantity-btn:hover {
        background: linear-gradient(135deg, #2980b9, #1f618d);
        transform: scale(1.1);
    }
    
    /* 加载状态样式 */
    .loading {
        position: relative;
        pointer-events: none;
    }
    
    .loading::after {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        width: 20px;
        height: 20px;
        margin: -10px 0 0 -10px;
        border: 2px solid #fff;
        border-top: 2px solid transparent;
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }
    
    @@keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    
    /* 响应式设计 */
    @@media (max-width: 768px) {
        .production-reporting-container {
            padding: 10px;
        }
        
        .page-header, .form-card, .tabs-container {
            margin-bottom: 15px;
            padding: 20px;
        }
        
        .page-title {
            font-size: 24px;
        }
        
        .form-row {
            flex-direction: column;
            gap: 15px;
        }
        
        .btn-group {
            flex-direction: column;
            gap: 10px;
        }
        
        .btn {
            width: 100%;
        }
        
        .tabs-header {
            flex-direction: column;
        }
        
        .tab-item {
            text-align: left;
        }
    }
</style>

<div class="production-reporting-container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h1 class="page-title">
            <i class="fas fa-@(isViewMode ? "eye" : (Model?.Id > 0 ? "edit" : "plus"))"></i>
            @pageTitle
        </h1>
        <div class="page-subtitle">@(isViewMode ? "查看生产报工记录详细信息" : (Model?.Id > 0 ? "修改生产报工记录信息" : "填写新的生产报工记录"))</div>
    </div>
    
    <!-- 表单区域 -->
    <div class="form-card">
        <div class="form-section-title">
            <i class="fas fa-clipboard-list"></i>
            基本信息
        </div>
        
        <form id="productionReportingForm">
            <input type="hidden" id="reportingId" value="@(Model?.Id ?? 0)" />
            
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label required">
                        <i class="fas fa-tag"></i>
                        报工类型
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-clipboard"></i>
                        <input type="text" class="form-input" id="reportingType" value="@(Model?.ReportingType ?? "")" @(isViewMode ? "disabled" : "") placeholder="请输入报工类型">
                    </div>
                </div>
                <div class="form-group">
                    <label class="form-label required">
                        <i class="fas fa-barcode"></i>
                        生产工单编号
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-file-alt"></i>
                        <input type="text" class="form-input" id="productionWorkOrderCode" value="@(Model?.ProductionWorkOrderCode ?? "")" @(isViewMode ? "disabled" : "") placeholder="请输入生产工单编号">
                    </div>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">
                        <i class="fas fa-ruler"></i>
                        单位
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-balance-scale"></i>
                        <input type="text" class="form-input" id="unit" value="PCS" @(isViewMode ? "disabled" : "") placeholder="单位">
                    </div>
                </div>
                <div class="form-group">
                    <label class="form-label">
                        <i class="fas fa-cogs"></i>
                        生产工序
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-industry"></i>
                        <input type="text" class="form-input" id="productionProcess" value="" @(isViewMode ? "disabled" : "") placeholder="请输入生产工序">
                    </div>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label required">
                        <i class="fas fa-user"></i>
                        报工人
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-user-tie"></i>
                        <input type="text" class="form-input" id="reporter" value="@(Model?.Reporter ?? "")" @(isViewMode ? "disabled" : "") placeholder="请输入报工人">
                    </div>
                </div>
                <div class="form-group">
                    <label class="form-label">
                        <i class="fas fa-user-check"></i>
                        审核人
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-user-shield"></i>
                        <input type="text" class="form-input" id="approver" value="@(Model?.Approver ?? "")" @(isViewMode ? "disabled" : "") placeholder="请输入审核人">
                    </div>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label required">
                        <i class="fas fa-cube"></i>
                        产品编码
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-qrcode"></i>
                        <input type="text" class="form-input" id="productMaterialCode" value="@(Model?.ProductMaterialCode ?? "")" @(isViewMode ? "disabled" : "") placeholder="请输入产品编码">
                    </div>
                </div>
                <div class="form-group">
                    <label class="form-label required">
                        <i class="fas fa-box"></i>
                        产品名称
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-tag"></i>
                        <input type="text" class="form-input" id="productMaterialName" value="@(Model?.ProductMaterialName ?? "")" @(isViewMode ? "disabled" : "") placeholder="请输入产品名称">
                    </div>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label required">
                        <i class="fas fa-calculator"></i>
                        报工数量
                    </label>
                    <div class="input-with-icon">
                        <i class="fas fa-hashtag"></i>
                        <input type="number" class="form-input" id="reportingQuantity" value="@(Model?.ReportingQuantity ?? 0)" @(isViewMode ? "disabled" : "") placeholder="请输入报工数量" min="1">
                    </div>
                </div>
                <div class="form-group">
                    <label class="form-label">
                        <i class="fas fa-vial"></i>
                        待检测数量
                    </label>
                    <div class="quantity-input-group">
                        <div class="input-with-icon" style="flex: 1;">
                            <i class="fas fa-microscope"></i>
                            <input type="number" class="form-input" id="pendingQuantity" value="0" @(isViewMode ? "disabled" : "") placeholder="待检测数量" min="0">
                        </div>
                        @if (!isViewMode)
                        {
                            <button type="button" class="quantity-btn" onclick="addPendingQuantity()">
                                <i class="fas fa-plus"></i>
                            </button>
                        }
                    </div>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">
                        <i class="fas fa-info-circle"></i>
                        规格型号
                    </label>
                    <textarea class="form-textarea" id="productModel" @(isViewMode ? "disabled" : "") placeholder="请输入规格型号">@(Model?.ProductModel ?? "")</textarea>
                </div>
                <div class="form-group">
                    <label class="form-label">
                        <i class="fas fa-comment-alt"></i>
                        备注
                    </label>
                    <textarea class="form-textarea" id="remark" @(isViewMode ? "disabled" : "") placeholder="请输入备注">@(Model?.Remark ?? "")</textarea>
                </div>
            </div>
            
            <div class="btn-group">
                @if (!isViewMode)
                {
                    <button type="button" class="btn btn-primary" onclick="saveProductionReporting()">
                        <i class="fas fa-save"></i>
                        保存
                    </button>
                }
                <button type="button" class="btn btn-default" onclick="goBack()">
                    <i class="fas fa-arrow-left"></i>
                    返回
                </button>
            </div>
        </form>
    </div>
    
    <!-- 标签页区域 -->
    <div class="tabs-container">
        <div class="tabs-header">
            <div class="tab-item active" data-tab="materials">
                <i class="fas fa-boxes"></i>
                生产资料
            </div>
            <div class="tab-item" data-tab="waste">
                <i class="fas fa-trash-alt"></i>
                生产废料
            </div>
        </div>
        
        <div class="tab-content">
            <div id="materials-tab" class="tab-pane active">
                <table class="table">
                    <thead>
                        <tr>
                            <th><i class="fas fa-receipt"></i> 退料单编号</th>
                            <th><i class="fas fa-file-alt"></i> 退料单名称</th>
                            <th><i class="fas fa-clipboard-list"></i> 生产工单</th>
                            <th><i class="fas fa-calendar-alt"></i> 退料日期</th>
                            <th><i class="fas fa-flag"></i> 单据状态</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="5" class="empty-data">
                                <i class="fas fa-inbox"></i>
                                <span>暂无生产资料数据</span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <div id="waste-tab" class="tab-pane">
                <table class="table">
                    <thead>
                        <tr>
                            <th><i class="fas fa-receipt"></i> 废料单编号</th>
                            <th><i class="fas fa-file-alt"></i> 废料单名称</th>
                            <th><i class="fas fa-clipboard-list"></i> 生产工单</th>
                            <th><i class="fas fa-calendar-alt"></i> 废料日期</th>
                            <th><i class="fas fa-flag"></i> 单据状态</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="5" class="empty-data">
                                <i class="fas fa-inbox"></i>
                                <span>暂无生产废料数据</span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
    // 标签页切换
    document.querySelectorAll('.tab-item').forEach(function(tab) {
        tab.addEventListener('click', function() {
            var targetTab = this.getAttribute('data-tab');
            
            // 移除所有active类
            document.querySelectorAll('.tab-item').forEach(function(item) {
                item.classList.remove('active');
            });
            document.querySelectorAll('.tab-pane').forEach(function(pane) {
                pane.classList.remove('active');
            });
            
            // 添加active类到当前选中的标签和内容
            this.classList.add('active');
            document.getElementById(targetTab + '-tab').classList.add('active');
        });
    });
    
    // 增加待检测数量
    function addPendingQuantity() {
        var pendingInput = document.getElementById('pendingQuantity');
        var currentValue = parseInt(pendingInput.value) || 0;
        pendingInput.value = currentValue + 1;
        
        // 添加动画效果
        pendingInput.style.transform = 'scale(1.1)';
        setTimeout(function() {
            pendingInput.style.transform = 'scale(1)';
        }, 200);
    }
    
    // 保存生产报工
    function saveProductionReporting() {
        // 验证必填字段
        var reportingType = document.getElementById('reportingType').value.trim();
        var productionWorkOrderCode = document.getElementById('productionWorkOrderCode').value.trim();
        var productMaterialCode = document.getElementById('productMaterialCode').value.trim();
        var productMaterialName = document.getElementById('productMaterialName').value.trim();
        var reportingQuantity = parseInt(document.getElementById('reportingQuantity').value) || 0;
        var reporter = document.getElementById('reporter').value.trim();
        
        // 验证逻辑
        var validationErrors = [];
        
        if (!reportingType) {
            validationErrors.push('报工类型不能为空');
        }
        
        if (!productionWorkOrderCode) {
            validationErrors.push('生产工单编号不能为空');
        }
        
        if (!productMaterialCode) {
            validationErrors.push('产品编码不能为空');
        }
        
        if (!productMaterialName) {
            validationErrors.push('产品名称不能为空');
        }
        
        if (reportingQuantity <= 0) {
            validationErrors.push('报工数量必须大于0');
        }
        
        if (!reporter) {
            validationErrors.push('报工人不能为空');
        }
        
        if (validationErrors.length > 0) {
            alert('请修正以下错误：\n' + validationErrors.join('\n'));
            return;
        }
        
        var data = {
            Id: parseInt(document.getElementById('reportingId').value) || 0,
            ReportingType: reportingType,
            ProductionWorkOrderCode: productionWorkOrderCode,
            ProductMaterialCode: productMaterialCode,
            ProductMaterialName: productMaterialName,
            ProductModel: document.getElementById('productModel').value.trim(),
            ReportingQuantity: reportingQuantity,
            Reporter: reporter,
            ReportingTime: new Date().toISOString(),
            Approver: document.getElementById('approver').value.trim(),
            Remark: document.getElementById('remark').value.trim(),
            Recorder: 'admin'
        };
        
        var url = data.Id > 0 ? '/ProductionSystem/ProductionReporting/UpdateProductionReporting' : '/ProductionSystem/ProductionReporting/AddProductionReporting';
        
        // 显示加载状态
        var saveBtn = event.target;
        var originalText = saveBtn.innerHTML;
        saveBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 保存中...';
        saveBtn.disabled = true;
        saveBtn.classList.add('loading');
        
        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]')?.value || ''
            },
            body: JSON.stringify(data)
        })
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.json();
        })
        .then(result => {
            if (result.code === 0) {
                // 成功动画
                saveBtn.innerHTML = '<i class="fas fa-check"></i> 保存成功';
                saveBtn.style.background = 'linear-gradient(135deg, #27ae60, #2ecc71)';
                
                setTimeout(function() {
                    goBack();
                }, 1000);
            } else {
                throw new Error(result.msg || '未知错误');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('保存失败：' + error.message);
        })
        .finally(() => {
            // 恢复按钮状态
            setTimeout(function() {
                saveBtn.innerHTML = originalText;
                saveBtn.disabled = false;
                saveBtn.classList.remove('loading');
                saveBtn.style.background = '';
            }, 2000);
        });
    }
    
    // 返回列表页
    function goBack() {
        // 添加页面切换动画
        document.body.style.opacity = '0.5';
        setTimeout(function() {
            window.location.href = '/ProductionSystem/ProductionReporting/Index';
        }, 300);
    }
    
    // 页面加载动画
    document.addEventListener('DOMContentLoaded', function() {
        // 添加页面加载动画
        document.body.style.opacity = '0';
        setTimeout(function() {
            document.body.style.transition = 'opacity 0.5s ease';
            document.body.style.opacity = '1';
        }, 100);
        
        // 表单验证增强
        var inputs = document.querySelectorAll('.form-input, .form-textarea');
        inputs.forEach(function(input) {
            input.addEventListener('blur', function() {
                if (this.hasAttribute('required') || this.classList.contains('required')) {
                    if (!this.value.trim()) {
                        this.style.borderColor = '#e74c3c';
                        this.style.boxShadow = '0 0 0 3px rgba(231, 76, 60, 0.1)';
                    } else {
                        this.style.borderColor = '#27ae60';
                        this.style.boxShadow = '0 0 0 3px rgba(39, 174, 96, 0.1)';
                    }
                }
            });
            
            input.addEventListener('focus', function() {
                this.style.borderColor = '#3498db';
                this.style.boxShadow = '0 0 0 3px rgba(52, 152, 219, 0.1)';
            });
        });
    });
</script> 